package com.yenlien.traceyun.business.service;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.controller.response.BaseSoilView;
import com.yenlien.traceyun.business.dao.BaseSoilDao;
import com.yenlien.traceyun.business.dao.BaseSoilEquipmentDao;
import com.yenlien.traceyun.business.dao.CompanyShowDao;
import com.yenlien.traceyun.business.dao.EquipmentDao;
import com.yenlien.traceyun.business.entity.BaseSoil;
import com.yenlien.traceyun.business.entity.BaseSoilEquipment;
import com.yenlien.traceyun.business.entity.CompanyShow;
import com.yenlien.traceyun.business.service.rest.usercenter.CompanyRestService;
import com.yenlien.traceyun.common.utils.ToolsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2021-01-04
 * @description:
 **/

@Service
public class CompanyShowService {

    @Autowired
    private CompanyShowDao companyShowDao;

    @Autowired
    private CompanyRestService companyRestService;

    @Autowired
    private BaseSoilDao baseSoilDao;

    @Autowired
    private EquipmentDao equipmentDao;

    @Autowired
    private BaseSoilEquipmentDao baseSoilEquipmentDao;

    @Value("${traceyun.app.id}")
    private String appId;


    public Object show(String code) {
        CompanyShow companyShow = companyShowDao.findByCode(code);
        if (companyShow == null) {
            return null;
        }
        JSONObject companyResult = (JSONObject) companyRestService.findById(companyShow.getCompanyId());
        JSONObject result = new JSONObject();
        result.put("show", companyShow);
        JSONObject companyJson = companyResult.getJSONObject("data");
        result.put("company", companyJson);
//            List<BaseSoil> baseSoils = baseSoilDao.findByCompanyIdAndAppId(companyShow.getCompanyId(), appId);
        List<BaseSoil> baseSoils = baseSoilDao.findByCompanyId(companyShow.getCompanyId());
        result.put("basesoil", baseSoils);
        if (companyShow.isShowVideo()) {
            List<BaseSoilView> views = new ArrayList<>();
            if (baseSoils != null && baseSoils.size() > 0) {
                for (BaseSoil baseSoil : baseSoils) {
                    BaseSoilView view = new BaseSoilView();
                    BeanUtils.copyProperties(baseSoil, view);
                    List<BaseSoilEquipment> equipments = baseSoilEquipmentDao.findByBaseSoilId(view.getId());
                    view.setSoilEquipments(equipments);
                    views.add(view);
                }
                result.put("basesoil", views);
            }
        }
        if (companyShow.isShowWeather()) {
            String area = companyJson.getString("area");
            if (!StringUtils.isEmpty(area)) {
                String areacode = area.split(",")[2];
                result.put("weather", ToolsUtil.parseWeather(areacode));
            }
        }
        return result;
    }
}
